package com.java.core.regrex;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author YongQiang
 * @Date 2021/10/9 9:17
 * @Version 1.0
 */
public class RegrexTest {
    // (([1-9]|1[0-2]):([0-5][0-9]))[ap]m
    // 11:59am
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Pattern:");
        String patternString = in.nextLine();

        Pattern pattern = Pattern.compile(patternString);
        while (true) {
            System.out.println("Enter String to match:");
            String input = in.nextLine();
            if (input == null || input == "") {
                return;
            }
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()){
                System.out.println("Matches");
                int groupCount = matcher.groupCount();
                System.out.println("group 数量: " + groupCount);
/*            for (int i = 0; i < groupCount; i++) {
                String result = matcher.group(i);
                System.out.println("group "+i+" ("+result+")");
            }*/
                if (groupCount > 0 ){
                    for (int i = 0; i < input.length(); i++) {
                        for (int j = 0; j < groupCount; j++) {
                            if (i == matcher.start(j) && i == matcher.end(j)){
                                System.out.print("()");
                            }
                        }
                        for (int j = 0; j < groupCount; j++) {
                            if (i == matcher.start(j) && i != matcher.end(j)){
                                System.out.print("(");
                            }
                        }
                        System.out.print(input.charAt(i));
                        for (int j = 0; j < groupCount; j++) {
                            if (i != matcher.start(j) && i == matcher.end(j)){
                                System.out.print(")");
                            }
                        }
                    }
                }
                System.out.println();
/*                System.out.println(matcher.find());
                System.out.println(matcher.start());
                System.out.println(matcher.end());*/

            }else {
                System.out.println("not matches");
            }
        }
    }
}
